package sa.com.sure.ejtimaa.database;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.model.AttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingItemInfo;
import sa.com.sure.ejtimaa.model.MeetingItemsAttachmentInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class MeetingItemsAttachments {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "MeetingItemsAttachments";
	
    public static final String MeetingItemIDField = "MeetingItemID"; 
    public static final String AttachmentIDField = "AttachmentID";
    
    private String[] allColumns = { MeetingItemIDField, AttachmentIDField};
    
    Cursor cursor = null;
    
    public MeetingItemsAttachments(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static MeetingItemsAttachments Instance = new MeetingItemsAttachments(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
		
	
	public MeetingItemsAttachmentInfo GetItem(int MeetingItemID , String AttachmentID) throws SQLException
	{	
		try	
		{
			open();
		
			MeetingItemsAttachmentInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingItemIDField+ "=" + MeetingItemID +" and " + AttachmentIDField +" = '" + AttachmentID + "'",null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		item =  getItemInfo(cursor);	     
    	        
        	return 	item;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
		
	
	public List<MeetingItemsAttachmentInfo> GetItemsByMeetingItemID(int MeetingItemID) throws SQLException
	{	
		try	
		{
			open();
		
			List<MeetingItemsAttachmentInfo> items = new ArrayList<MeetingItemsAttachmentInfo>();
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingItemIDField+ "=" + MeetingItemID,null,null,null,null);
		    
        	if (cursor.moveToFirst()) 
        	do{        		
        		items.add(getItemInfo(cursor));        		
        	}
        	while(cursor.moveToNext());	     
    	        
        	return 	items;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
	
	public void addItem(MeetingItemsAttachmentInfo item) throws IOException 
	{ 
		try	
		{        
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(MeetingItemIDField, item.GetMeetingItemID());  
	        values.put(AttachmentIDField, item.GetAttachmentID());  
	        
	        SaveFile(item);
	        
	        dbHelper.db.insert(TABLE_Name, null, values); 
	        
	        Attachments.Instance.addItem(item);
        
		}
	    finally{        	
	        close();
	    }  
                
    } 
		
	
    private MeetingItemsAttachmentInfo getItemInfo(Cursor cursor) {  
    	
    	MeetingItemsAttachmentInfo itemInfo = new MeetingItemsAttachmentInfo();
    	
    	itemInfo.SetMeetingItemID(cursor.getInt(cursor.getColumnIndex(MeetingItemIDField)));    	
    	itemInfo.SetAttachmentID(cursor.getString(cursor.getColumnIndex(AttachmentIDField)));    	
    	
    	AttachmentInfo attachment = Attachments.Instance.GetItem(itemInfo.GetAttachmentID());    	
    	
    	itemInfo.SetFileName(attachment.GetFileName());
    	itemInfo.SetFileSize(attachment.GetFileSize());
    	itemInfo.SetFileType(attachment.GetFileType());    
    	itemInfo.SetFileContent(attachment.GetFileContent());    	
    	
		return itemInfo;
	}
    
    private boolean SaveFile(MeetingItemsAttachmentInfo item) throws IOException{
    	
    	MeetingItemInfo meetingItem = MeetingItems.Instance.GetItem(item.GetMeetingItemID());
    	
    	String path = String.format("/%s/%s/" ,meetingItem.GetMeetingID(),item.GetMeetingItemID());
    	Helper.SaveFile(path, item.GetFileName(), item.GetFileContent());
    	
    	return true;
    }
    
    public String GetFilePath(int meetingItemID){
    	
    	MeetingItemInfo meetingItem = MeetingItems.Instance.GetItem(meetingItemID);
    	
    	String path = String.format("/%s/%s/" ,meetingItem.GetMeetingID(),meetingItemID);
    
    	return path;
    }
}
